<?php
namespace Core\Validation\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;

class FunctionValidator extends Validator implements ValidatorInterface
{
    /**
     * 执行验证
     *
     * @param Phalcon\Validation $validator
     * @param string $attribute
     * @return boolean
     */
    public function validate(Validation $validator, $attribute)
    {
        $value = $validator->getValue($attribute);
        $function = $this->getOption('callable');
        if (call_user_func($function, $value) == false) {

            $message = $this->getOption('message');
            if (!$message) {
                $message = '验证码输入错误';
            }

            $validator->appendMessage(new Message($message, $attribute, 'Function'));

            return false;
        }

        return true;
    }
}
